﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 流程可见
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018.01.17 
    /// </summary>
    internal class FlowEngineVisibleService : DataFactory, FlowEngineVisibleIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="flowId">流程主键</param>
        /// <returns></returns>
        public async Task<List<FlowEngineVisibleEntity>> GetList(string flowId)
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<FlowEngineVisibleEntity>().Where(m => m.FlowId == flowId).OrderBy(o => o.SortCode).ToListAsync();

            }
        }
        /// <summary>
        /// 可见流程列表
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<List<FlowEngineVisibleEntity>> GetVisibleFlowList(string userId)
        {
            using (var db = this.Basic)
            {
                var userRelationList = (await db.GetListAsync<UserRelationEntity>(m => m.UserId == userId)).Select(m => m.ObjectId).ToList();
                userRelationList.Add(userId);
                var operatorId = string.Join("','", userRelationList.Distinct());
                return await db.GetListSqlAsync<FlowEngineVisibleEntity>(string.Format("select * from Flow_EngineVisible WHERE F_OperatorId IN ('{0}') ", operatorId));
            }
        }
    }
}
